<?php

class JoinController extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('Article');
        Zend_Loader::loadClass('Phrase');
    }
    
    function preDispatch()
    {
        $Article = new Article();
        $this->view->main_menu = $Article->getPublished();
        
        $Phrase = new Phrase();
        $this->view->phrase = $Phrase->getRandom();
    }
        
    function indexAction()
    {
        Zend_Session::start();
        
        if(!isset($_SESSION['lang'])) $_SESSION['lang'] = Translation::getDefaultLanguage();
        
        $this->view->new_id = 0;
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $params['authgroup_id'] = 1; // student
            $params['first_name'] = trim($filter->filter($this->_request->getPost('first_name')));
            $params['last_name'] = trim($filter->filter($this->_request->getPost('last_name')));
            $params['address'] = trim($filter->filter($this->_request->getPost('address')));
            $params['contacts'] = trim($filter->filter($this->_request->getPost('contacts')));
            $params['email'] = trim($filter->filter($this->_request->getPost('email')));
            $params['phone'] = trim($filter->filter($this->_request->getPost('phone')));
            $params['birthday'] = trim($filter->filter($this->_request->getPost('birthday')));
            $params['register_date'] = date('Y-m-d H:i:s');
            
            $obj = new User();
            $this->view->message = $obj->validate(0,$params);
            if($this->view->message===true) {
                $this->view->new_id = $obj->insert($params);
                if($this->view->new_id>0) {
                    mail(ADMIN_EMAIL,'Subject A','Message',"From: " . ADMIN_EMAIL);
                    mail($params['email'],'Subject B','Message',"From: " . ADMIN_EMAIL);
                }
            }
        }
        
        $this->view->f_user = new stdClass();
        $this->view->f_user->id = 0;
        $this->view->f_user->first_name = isset($params['first_name'])?$params['first_name']:'';
        $this->view->f_user->last_name = isset($params['last_name'])?$params['last_name']:'';
        $this->view->f_user->address = isset($params['address'])?$params['address']:'';
        $this->view->f_user->contacts = isset($params['contacts'])?$params['contacts']:'';
        $this->view->f_user->email = isset($params['email'])?$params['email']:'';
        $this->view->f_user->phone = isset($params['email'])?$params['phone']:'';
        $this->view->f_user->birthday = isset($params['birthday'])?$params['birthday']:'';
        
        $this->render();        
    }
    
}